﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	public partial class PipeSlopeForm : System.Windows.Forms.Form
	{
		public PipeSlopeForm(UserSetting userSetting)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.userSettingS = userSetting;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.txtSlope.Text.Trim() == "")
			{
				MessageBox.Show("参数不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				return;
			}
			this.slope = double.Parse(this.txtSlope.Text);
			if (this.txtStart.Text.Trim() != "")
			{
				this.startElevation = this.txtStart.Text;
			}
			this.WriteParameterSetting();
			base.DialogResult = DialogResult.OK;
		}

		private void ReadParameterSetting()
		{
			this.txtStart.Text = this.userSettingS.GetValue(this.xmlbootname, "startElevation");
			this.txtSlope.Text = this.userSettingS.GetValue(this.xmlbootname, "slope");
		}

		private void WriteParameterSetting()
		{
			this.userSettingS.WriteValue(this.xmlbootname, "startElevation", this.txtStart.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "slope", this.txtSlope.Text.Trim());
		}

		private void txtSlope_KeyPress(object sender, KeyPressEventArgs e)
		{
			PipeSlopeForm.OnlyEnterNumber(this.txtSlope, e);
		}

		private void txtStart_KeyPress(object sender, KeyPressEventArgs e)
		{
			PipeSlopeForm.OnlyEnterNumber(this.txtStart, e);
		}

		public static void OnlyEnterNumber(System.Windows.Forms.TextBox txt, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '-' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '-' && (txt.SelectionStart != 0 || txt.Text.IndexOf("-") >= 0))
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.Text.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private void PipeSlopeForm_Load(object sender, EventArgs e)
		{
			this.ReadParameterSetting();
		}

		private UserSetting userSettingS;

		private string xmlbootname = "PipeSlope";

		public double slope;

		public string startElevation = "";
	}
}
